package com.bsi.core.exception;

import lombok.Data;
/**
 * 异步提交后返回的内容
 *
 * @author lipan
 */
@Data
public class ResponseEntity<T> {
    /**
     * 状态,1,成功，0.失败
     */
    private String status = "1";
    /**
     * 错误代码,业务系统返回到前端后，判断这个来处理逻辑
     */
    private Integer code = 0;
    /**
     * 简单的错误信息
     */
    private String errorMessage;
    /**
     * 详细的堆栈错误信息
     */
    private String detailErrorMessage;
    /**
     * 异常发生时间
     */
    private String errDate;
    /**
     * 业务数据
     */
    private T data;

    private ResponseEntity(T data) {
        this.code = SystemCode.GoodRequest.getCode();
        this.data = data;
    }

    public ResponseEntity() {
    }

    private ResponseEntity(Integer code, String errorMessage) {
        this.status = "0";
        this.code = code;
        this.errorMessage = errorMessage;
    }

    /**
     * 返回异常信息
     *
     * @param code 错误码
     * @return
     */
    public static ResponseEntity createError(Code code) {
        return new ResponseEntity(code.getCode(), code.getMessage());
    }

    /**
     * 返回错误码
     *
     * @param code         异常Code的枚举值
     * @param errorMessage 自定义的异常信息
     * @return
     */
    public static ResponseEntity createError(Code code, String errorMessage) {
        return new ResponseEntity(code.getCode(), String.format(code.getMessage(), errorMessage));
    }

    /**
     * 返回正常信息
     *
     * @param data 参数
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity createSuccess(T data) {
        return new ResponseEntity(data);
    }

    /**
     * 返回正常信息
     */
    public static ResponseEntity createSuccess() {
        return new ResponseEntity();
    }
}
